package model;

import java.util.LinkedList;
import java.util.Observable;

import util.StateCode;

public class ChessArray extends Observable {
	public Chess[][] chessarray = new Chess[15][15];
	public static final int COLUMNS = 15;
	public static final int ROWS = 15;
	public static LinkedList<Chess> chesslist;
	private ChessArray ca;
	public ChessArray() {
		chesslist=new LinkedList<>();
		ca=this;
	}

	public boolean saveChess(Chess chess) {
		int i = chess.getY();
		int j = chess.getX();
		if (chessarray[i][j] == null) {
			chessarray[i][j] = chess;
			chesslist.offer(chess);
			setChanged();
			notifyObservers("savesuccess");
			if (finish(chess)) {
				setChanged();
				notifyObservers(chess.getccolor());
			}
			return true;
		}
		return false;
	}

	public Chess getChess(int i, int j) {
		return chessarray[i][j];
	}

	public  void clearboard() {
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 15; j++) {
				chessarray[i][j] = null;
			}
		}
	}

	public boolean finish(Chess chess) {
		int row = chess.getY();
		int col = chess.getX();
		int c = 1;
		for (int j = col + 1; j < COLUMNS && chessarray[row][j] != null && chess.equalColor(chessarray[row][j]); j++)
			c++;

		for (int j = col - 1; j >= 0 && chessarray[row][j] != null && chess.equalColor(chessarray[row][j]); j--)
			c++;

		if (c >= 5)
			return true;

		c = 1;

		for (int j = row + 1; j < COLUMNS && chessarray[j][col] != null && chess.equalColor(chessarray[j][col]); j++)
			c++;

		for (int j = row - 1; j >= 0 && chessarray[j][col] != null && chess.equalColor(chessarray[j][col]); j--)
			c++;

		if (c >= 5)
			return true;

		c = 1;

		for (int i = row + 1, j = col + 1; j < COLUMNS && i < ROWS && chessarray[i][j] != null
				&& chess.equalColor(chessarray[i][j]); i++, j++)
			c++;

		for (int i = row - 1, j = col - 1; j >= 0 && i >= 0 && chessarray[i][j] != null
				&& chess.equalColor(chessarray[i][j]); i--, j--)
			c++;

		if (c >= 5)
			return true;

		c = 1;

		for (int i = row - 1, j = col + 1; j < COLUMNS && i >= 0 && chessarray[i][j] != null
				&& chess.equalColor(chessarray[i][j]); i--, j++)
			c++;

		for (int i = row + 1, j = col - 1; j >= 0 && i < ROWS && chessarray[i][j] != null
				&& chess.equalColor(chessarray[i][j]); i++, j--)
			c++;

		if (c >= 5)
			return true;

		return false;

	}
	public void remove(){
		Chess chess1=chesslist.removeLast();
		int i = chess1.getY(), j = chess1.getX();
		chessarray[i][j] = null;
		Chess chess2=chesslist.removeLast();
		i=chess2.getY();j=chess2.getX();
		chessarray[i][j]=null;
	}
	public void replay(){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
					ca.clearboard();
					for (Chess chesses : chesslist) {
						int i=chesses.getY(), j=chesses.getX();
						ca.chessarray[i][j]=chesses;
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				}
					setChanged();
					notifyObservers(StateCode.REPLAYDONE);
				
			}
		}).start();
	}
}
